package com.example.xwweibo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xwcommon.entity.Comment;
import com.example.xwcommon.service.CommentService;
import com.example.xwcommon.vo.Response;
import com.example.xwweibo.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController(value = "commentService")
public class CommentServiceImpl implements CommentService{
    @Autowired
    private CommentMapper commentMapper;

    @Override
    @Transactional
    public Response<Comment> addComment(Integer wbid, String content, String userloginname) {
        try{
            Comment comment = new Comment();
            comment.setWeiboId(wbid);
            comment.setContent(content);
            comment.setUserName(userloginname);
            comment.setState(1);
            comment.setCreateTime(new java.util.Date());
            if(commentMapper.insert(comment) ==1){
                return new Response<>(200,"评论成功",comment);
            }
            return new Response<>(500,"评论失败",null);
        }
        catch(Exception e){
            return new Response<>(500,e.getMessage(),null);
        }
    }

    @Override
    public Response<Page<Comment>> showListByWeibo(Integer wbid, Integer pageNum, Integer pageSize, Integer state) {
        Page<Comment> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        if(wbid!=null){
            queryWrapper.eq("cm_weiboid",wbid);
        }
        if(state!=null){
            queryWrapper.eq("cm_state",state);
        }
        Page<Comment> commentPage = commentMapper.selectPage(page, queryWrapper);
        if(commentPage!=null){
            return new Response<>(200,"微博id查询评论列表成功",commentPage);
        }
        return new Response<>(500,"微博id查询评论列表失败",null);
    }

    @Override
    public Response<Page<Comment>> showListByUser(String userloginname, Integer pageNum, Integer pageSize, Integer state) {
        Page<Comment> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        if(userloginname!=null){
            queryWrapper.eq("cm_user_loginname",userloginname);
        }
        if(state!=null){
            queryWrapper.eq("cm_state",state);
        }
        Page<Comment> commentPage = commentMapper.selectPage(page, queryWrapper);
        if(commentPage!=null){
            return new Response<>(200,"用户名查询评论列表成功",commentPage);
        }
        return new Response<>(500,"用户名查询评论列表失败",null);
    }

    @Override
    public Response<Integer> removeComment(Integer cmid) {
        if(commentMapper.deleteById(cmid)==1){
            return new Response<>(200,"删除评论成功",cmid);
        }
        return new Response<>(500,"删除评论失败",null);
    }

    @Override
    public Response<Comment> updateState(Integer cmId,Integer state){
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cm_id",cmId);
        Comment comment = commentMapper.selectOne(queryWrapper);
        comment.setState(state);
        if(commentMapper.updateById(comment)==1){
            return new Response<>(200,"更新评论状态成功",comment);
        }
        return new Response<>(500,"更新评论状态失败",null);
    }
}
